$(document).ready(function (){
    var news_url = '/index/web/news'
    attrArray = []
    product_page_index = 1
    var cat_id = $(".news_category_id").val();
    var app_page = $(".app_page").val();
    var news_cat_id = cat_id;

    $(".news-cate-tag").on("click",function (){
        var news_cat_id = $(this).data('id')
        newsPage(1,news_cat_id,app_page)
    })

    newsPage(1,news_cat_id,app_page);

    function newsPage(state,cat_id = '',page = 3){
        $.ajax({
            type: 'POST',
            url: news_url,
            data: {"page_index":1,"page_size":page,"cat_id":cat_id},
            success: function(res){
                if(res.code == 0){
                    if(state == 1){
                        $.ajax({
                            type: 'POST',
                            url: news_url,
                            data: {"page_index":1,"page_size":page,"cat_id":cat_id},
                            success: function(res){
                                if(res.code == 0){
                                    if(res.data.list.length == 0){
                                        //alert('已经没有数据了')
                                        $(".news-list").html();
                                    }else{
                                        $(".news-content").html('');
                                        $.each(res.data.list,function (k,v){
                                            var html = '<a href="/articledetail/' + v.article_id + '.html" class="n-li">\n' +
                                                '          <div class="toppic">\n' +
                                                '            <img src="'+v.photo+'" alt="">\n' +
                                                '          </div>\n' +
                                                '          <div class="infobox">\n' +
                                                '            <h5 class="tit dot2">'+v.title+'</h5>\n' +
                                                '            <p class="txt  dot2">'+v.content2+'</p>\n' +
                                                '            <div class="time">\n' +
                                                '              <img src="images/clock.png" alt=""><span>'+v.create_time+'</span>\n' +
                                                '            </div>\n' +
                                                '          </div>\n' +
                                                '\n' +
                                                '        </a>';

                                            $(".news-content").append(html)
                                        })
                                        $(".news-list").html($(".news-content").html());
                                    }
                                }else{
                                    alert(res.msg)
                                }
                            },
                            timeout: 2000,
                            error: function(jqXHR,textStatus){
                                console.log("请求失败，请稍后重试!");
                            }
                        })
                    }
                    layui.use('laypage', function(){
                        var laypage = layui.laypage;
                        //执行一个laypage实例
                        laypage.render({
                            elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                            ,limit: page
                            ,count: res.data.count //数据总数，从服务端得到
                            ,groups:5
                            ,prev:"Previous"
                            ,next:"Next"
                            ,jump: function(obj, first){
                                //首次不执行
                                if(!first){
                                    //data: {"page_index":obj.curr,"page_size":obj.limit,"cat_id":cat_id},
                                    //ajax request begin
                                    $.ajax({
                                        type: 'POST',
                                        url: news_url,
                                        data: {"page_index":obj.curr,"page_size":obj.limit,"cat_id":cat_id},
                                        success: function(res){
                                            if(res.code == 0){
                                                if(res.data.list.length == 0){
                                                    //alert('已经没有数据了')
                                                    $(".news-list").html('');
                                                }else{
                                                    $(".news-content").html('');
                                                    $.each(res.data.list,function (k,v){
                                                        var html = '<a href="/articledetail/' + v.article_id + '.html" class="n-li">\n' +
                                                            '          <div class="toppic">\n' +
                                                            '            <img src="'+v.photo+'" alt="">\n' +
                                                            '          </div>\n' +
                                                            '          <div class="infobox">\n' +
                                                            '            <h5 class="tit dot2">'+v.title+'</h5>\n' +
                                                            '            <p class="txt  dot2">'+v.content2+'</p>\n' +
                                                            '            <div class="time">\n' +
                                                            '              <img src="images/clock.png" alt=""><span>'+v.create_time+'</span>\n' +
                                                            '            </div>\n' +
                                                            '          </div>\n' +
                                                            '\n' +
                                                            '        </a>';

                                                        $(".news-content").append(html)
                                                    })
                                                    $(".news-list").html($(".news-content").html());
                                                }
                                            }else{
                                                alert(res.msg)
                                            }
                                        },
                                        timeout: 2000,
                                        error: function(jqXHR,textStatus){
                                            console.log("请求失败，请稍后重试!");
                                        }
                                    })
                                }
                            }
                        });
                    })
                }else {
                    alert(res.msg)
                }
            },
            timeout: 2000,
            error: function(jqXHR,textStatus){
                console.log("请求失败，请稍后重试!");
            }
        })
    }
})

